﻿@page "/"
@inject IHttpClientFactory httpClientFactory
@inject ISnackbar Snackbar
@using System.Security.Cryptography.X509Certificates
@using System.Security.Cryptography
@using System.Net
@using System.Net.Security
@using System.Security.Authentication
@using System.Text.Json
@using System.Text
@using System.Net.Http.Headers
@using Tool.Models
@using Tool.Components.Pages.SmallTools
@inject IJSRuntime JSRuntime

<PageTitle>Evd</PageTitle>

<MudText Typo="Typo.h3" GutterBottom="true">苦海</MudText>
@* <MudText Typo="Typo.h5" GutterBottom="true">@_currentTime</MudText> *@
<MudGrid>
    <MudItem xs="12" Class="align-content-center">
        <Login  OnSuccess="HandleLogin"></Login>
    </MudItem>
    <MudItem xs="12" Class="align-content-center">
        <Customer SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders" OnGetCustomer="HandleCustomer"></Customer>
    </MudItem>
    <MudItem xs="12" Class="align-content-center">
        <File SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders" EvdNo="@_evdNo" GroupName="@_groupName" UserCode="@_userCode" AdmdivCode="@_admdivCode" CompanyCode="@_companyCode"></File>
    </MudItem>
    <MudItem xs="12" Class="align-content-center">
        <VoucherTerminal UserCode="@_userCode" SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders" GroupName="@_groupName"></VoucherTerminal>
    </MudItem>

    <MudItem xs="12" Class="align-content-center">
        <EvdPushLog EvdNo="@_evdNo" SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders" GroupName="@_groupName"></EvdPushLog>
    </MudItem>

    <MudItem xs="12" Class="align-content-center">
        <EvdSyncRecord EvdNo="@_evdNo" SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders" GroupName="@_groupName" UserCode="@_userCode"></EvdSyncRecord>
    </MudItem>
    <MudItem xs="12" Class="align-content-center">
        <TableStruct SelectedSite="@_selectedSite" ResponseHeaders="@_responseHeaders"></TableStruct>
    </MudItem>
</MudGrid>

@code {

    private bool _processing = false;
    private string _selectedSite = string.Empty;
    private HttpResponseHeaders _responseHeaders;
    private string _currentTime;
    private string _evdNo;
    private string _userCode;
    private string? _groupName;
    private string _companyCode;
    private string _admdivCode;



    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        // await GetCurrentTime();
    }

    private void HandleLogin(LoginResponseDto response)
    {
        _selectedSite = response.SelectedSite;
        _responseHeaders = response.ResponseHeaders;
    }

    private void HandleCustomer(CustomerDto customer)
    {
        _groupName = customer.GroupName;
        _userCode = customer.UserCode;
        _evdNo = $"BX{customer.FinAdmdivCode}{customer.CompanyCode}";
        _companyCode = customer.CompanyCode;
        _admdivCode = customer.AdmdivCode;
    }

    async Task GetCurrentTime()
    {
        _currentTime = await JSRuntime.InvokeAsync<string>("MyLib.GetCurrentTime");
        Console.WriteLine(_currentTime);
    }

}